/**
 * Copyright (c) 快宝网络 kuaidihelp.com Co., Ltd. All Rights Reserved 禁止外泄以及用于其它的商业用途
 */

const patterns = {
  phone: {
    // 手机号
    code: 3001,
    pattern: /^1[2-9]{1}[0-9]{1}\d{8}$/,
    message: "手机号码输入错误",
    tag: "手机号",
  },
  telPhone: {
    // 电话号码
    code: 3002,
    pattern: /^(0\d{2,3}(-|\s?))?[2-9]{1}\d{6,7}((-|\s?)\d{1,4})?$/,
    message: "电话号码输入错误",
    tag: "电话号码",
  },
  contact_blurry: {
    code: 300122,
    pattern: /^\d{4,11}$|^1[2-9]{1}[0-9]{1}\d{8}$|^(0\d{2,3}(-|\s?))?[2-9]{1}\d{6,7}((-|\s?)\d{1,4})?$/,
    message: "不符合指定规则",
    tag: "模糊的联系方式",
  },
  contact_4: {
    // 联系方式后四位
    code: 300121,
    pattern: /^\d{4}$/,
    message: "请输入完整手机、固话或号码后四位",
    tag: "联系方式",
  },
  contact: {
    // 联系方式
    code: 30012,
    pattern: /^1[2-9]{1}[0-9]{1}\d{8}$|^(0\d{2,3}(-|\s?))?[2-9]{1}\d{6,7}((-|\s?)\d{1,4})?$/,
    message: "请输入正确的手机或固话",
    tag: "联系方式",
  },
  decimal2: {
    // 支持小数 2位
    code: 3011,
    type: "number",
    pattern: /^\d+(?:\.\d{1,2})?$/,
    message: "数字且最多允许两位小数",
  },
  waybillNum: {
    // 运单号
    code: 3014,
    pattern: /^[0-9a-zA-Z\-]{4,30}$/,
    message: "单号不正确",
    tag: "单号",
  },
  orderNum: {
    // 订单号
    code: 3014,
    pattern: /^[0-9a-zA-Z\-]{4,30}$/,
    message: "订单号不正确",
    tag: "订单号",
  },
  bh: {
    // 取件码
    code: 3016,
    pattern: /^\d{6}$/,
    message: "包裹编号必须为6位数字",
    tag: "包裹编号",
  },
  cardId: {
    // 身份证号码
    code: 3017,
    pattern: /^(^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$)|(^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])((\d{4})|\d{3}[Xx])$)$/,
    message: "身份证号码不合法",
    tag: "身份证号码",
  },
  fresh: {
    // 生鲜包裹
    code: 3018,
    pattern: /^生鲜[:：]/,
    message: "不是生鲜物品",
    tag: "生鲜",
  },
  webUrl: {
    code: 3019,
    pattern: /^https?:\/\/(([a-zA-Z0-9_-])+(\.)?)*(:\d+)?(\/((\.)?(\?)?=?&?[a-zA-Z0-9_-](\?)?)*)*$/i,
    message: "链接不合法",
    tag: "链接",
  },
  cn: {
    code: 3020,
    pattern: /[a-zA-Z\u4e00-\u9fa5]/gm,
    message: "无中文字符",
    tag: "中文",
  },
};

const check = (key, value, special = {}) => {
  const rule = special[key] ||
    patterns[key] || {
      code: 11,
      message: `缺少对应的正则规则：${key}`,
    };
  const { pattern, message, tag } = rule;
  if (!pattern) {
    return rule;
  }
  let isExact = value.match(pattern);
  if (isExact) {
    return {
      ...rule,
      code: 0,
      message: "验证通过",
    };
  }
  return {
    code: 1001,
    ...rule,
    message: value ? message : `${tag}不能为空`,
  };
};

/**
 * 返回url协议与链接
 * @param {string} url URL链接
 * @param {string} type 要返回的类型 protocol | body (协议 | 链接)
 *  */
const spliceURL = (url = "", type = "protocol") => {
  if (!url) {
    return undefined;
  }
  const reg = /(http|https):\/\/([\w.]+\/?\S*)/;
  const result = url.match(reg) || [];

  const protocol = result[1] && result[1].toLowerCase();
  const urlBody = result[2];

  if (type === "protocol") {
    if (protocol) {
      return `${protocol}://`;
    }
    return undefined;
  } else if (type === "body" && urlBody) {
    return urlBody == "undefined" ? "" : urlBody;
  } else {
    return url;
  }
};

export { patterns as default, check, spliceURL };
